<?php
$sp_id = (int) $_GET['sp_id'];
$kg = $kieu -> getListKhoangGia();
$chitiet = $sp->getDetailSP($sp_id);
$row_chitiet = mysql_fetch_assoc($chitiet);
$listKieu = $kieu->listkieudang_byloai($row_chitiet['loai_id']);
$arrSize = $sp->getSizeIdByProductId($sp_id);
$arrMau = $sp->getMauIdByProductId($sp_id);
$arrChat = $sp->getChatIdByProductId($sp_id);
$arrKieu = $sp->getKieuIdByProductId($sp_id);
$arrTagVi = $sp->getTagsOfProductId($sp_id);

if (!empty($arrTagVi)) {
    $strTagVi = "";
    foreach ($arrTagVi as $tag_id) {
        $rs_tag = $sp->getDetailTag($tag_id);
        $row_tag = mysql_fetch_assoc($rs_tag);
        $strTagVi.=$row_tag["tag_name"] . "; ";
    }
}

if ($row_chitiet['hinh_anh'] != "")
    $arrImg = $sp->getArrImages($row_chitiet['hinh_anh']);

if ($row_chitiet['hinh_anh_vuong'] != "")
    $arrImg_vuong = $sp->getArrImages($row_chitiet['hinh_anh_vuong']);

$loai = $lsp->getListLoaiSP();
$listchat = $chat->getListChatLieu();

if (isset($_POST['btnSumit'])) {
    $thanhcong = $sp->SanPham_Sua($sp_id, $loi);
    if ($thanhcong == true) {
        header("location:?com=sp_list");
    }
}
?>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckfinder/ckfinder.js"></script>
<script>
    $(document).ready(function() {
        // ten kd
        $("input[name=ten_sp]").blur(function() {
            var ten_sp = $(this).val();
            $.post("blocks/ajax.php", {str: ten_sp}, function(data) {
                $("input[name=ten_sp_kd]").val(data);
            })
        })
        //check all
        $('#tags').on("keydown", function(event) {
            if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }
        }).autocomplete({
            source: function(request, response) {
                $.getJSON("ajax_tag.php", {
                    term: extractLast(request.term)
                }, response);
            },
            search: function() {
                // custom minLength
                var term = extractLast(this.value);
                if (term.length < 2) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function(event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join("; ");
                return false;
            }
        });
        $("#color_all").click(function()
        {
            var checked_status = this.checked;
            $("input.mau").each(function()
            {
                this.checked = checked_status;
            });
        });
        $("#size_all").click(function()
        {
            var checked_status = this.checked;
            $("input.size").each(function()
            {
                this.checked = checked_status;
            });
        });

        $('#upload_images').ajaxForm({
            beforeSend: function() {
            },
            uploadProgress: function(event, position, total, percentComplete) {

            },
            complete: function(data) {
                var arrRes = JSON.parse(data.responseText);
                alert(arrRes['thongbao']);
                $("#hinhanh").html(arrRes['text'] + arrRes['str_hinhanh']);
                $("#div_upload").dialog('close');
            }
        });
        $("#btnUpload").click(function() {
            $("#div_upload").dialog({
                modal: true,
                title: 'Upload images',
                width: 350,
                draggable: true,
                resizable: false,
                position: "center middle"
            });
        });
        $("#add_images").click(function() {
            $("#wrapper_input_files").append("<input type='file' name='images[]' /><br />");
        });
        
         $('#upload_images_2').ajaxForm({
            beforeSend: function() {
            },
            uploadProgress: function(event, position, total, percentComplete) {

            },
            complete: function(data) {
                var arrRes = JSON.parse(data.responseText);
                alert(arrRes['thongbao']);
                $("#hinhanh_2").html(arrRes['text'] + arrRes['str_hinhanh']);
                $("#div_upload_2").dialog('close');
            }
        });
        $("#btnUpload_2").click(function() {
            $("#div_upload_2").dialog({
                modal: true,
                title: 'Upload hình vuông',
                width: 350,
                draggable: true,
                resizable: false,
                position: "center middle"
            });
        });
        $("#add_images_2").click(function() {
            $("#wrapper_input_files_2").append("<input type='file' name='images[]' /><br />");
        });
    });
    function split(val) {
        return val.split(/;\s*/);
    }

    function extractLast(term) {
        return split(term).pop();
    }
</script>
<style>
    #div_upload{font-size:13px;text-align:center}
    #div_upload_2{font-size:13px;text-align:center}

</style>
<form action="" method="post" name="form_add_dm_ks">
    <div id="admin_navigation">
        <div style="float:left;width:90%">
            <h3>Quản lý sản phẩm: Thêm mới</h3>
        </div>
        <div style="float:left;width:5%;padding-top:5px">
            <input type="submit" class="save" name="btnSumit" value=""/><br />		
            <span>Save</span>
        </div>
        <div style="float:left;width:5%;padding-top:5px">
            <input type="reset" class="cancel" name="btnCancel" value=""/><br />		
            <span>Reset</span>
        </div>
        <div class="clr"></div>
    </div>
    <div id="main_admin">

        <div id="main_left" style="width:70%">
            <fieldset>
                <legend>Thông tin chung</legend>
                <table>                	
                    <tr>
                    	<td>Loại SP</td>
                        <td colspan="3">
                            <select name="loai_id" id="loai_id" style="width: 200px">
                                <?php while($row_loai = mysql_fetch_assoc($loai)){ ?>
                                <option <?php echo ($row_chitiet['loai_id']==$row_loai['loai_id'])? "selected=selected": ""; ?> value="<?php echo $row_loai['loai_id'];?>"><?php echo $row_loai['loai_vi'];?></option>
                                <?php } ?>
                            </select>                
                        </td>   
                    </tr>
                    <tr>
                    <td>Kiểu dáng</td>
                    <td colspan="3"><table style="width:300px" cellpadding="4" cellspacing="0">
                        <tr id="row_kieu">
                          <?php   while($row_kieu = mysql_fetch_assoc($listKieu)){
        ?>
                          <td style="width:20px;padding:4px;text-align:center;vertical-align: top;font-size: 12px;">
                              <input class="size" type="checkbox" name="kieu_id[]" value="<?php echo $row_kieu['kieu_id']; ?>"  
                                     <?php if (in_array($row_kieu['kieu_id'], $arrKieu) == true) echo "checked"; ?>
                                     />
                            <br />
                            <?php echo $row_kieu['kieu_vi']; ?></td>
                          <?php
        } 
        ?>
                        </tr>
                      </table></td>
                  </tr> 
                    
                    <tr>
                        <td>Chất liệu</td>
                        <td colspan="3">
                            <table style="width:500px" cellpadding="4" cellspacing="0">
                                <tr>
                                    <?php
                                    while ($row_chat = mysql_fetch_assoc($listchat)) {
                                        ?>
                                        <td style="width:20px;padding:0px;text-align:center;vertical-align: top;font-size: 12px;"><input class="size" type="checkbox" name="chat_id[]" value="<?php echo $row_chat['chat_id']; ?>" 
                                            <?php if (in_array($row_chat['chat_id'], $arrChat) == true) echo "checked"; ?>
                                                                                                                                         /><br /> <?php echo $row_chat['chat_vi']; ?></td>                               
                                        <?php } ?>
                                </tr>
                            </table>
                        </td>   
                    </tr>    
                          
                    <tr>
                        <td>Mã SP</td>
                        <td><input type="text" name="ma_sp" id="ma_sp" class="tf" value="<?php echo $row_chitiet['ma_sp'] ?>" /></td>                        
                    </tr>
                    <tr>
                        <td>Tên SP VI</td>
                        <td><input type="text" name="ten_sp_vi" id="ten_sp_vi" class="tf" value="<?php echo $row_chitiet['ten_sp_vi'] ?>" /></td>                        
                    </tr>
                    <tr>
                        <td>Tên SP KD VI</td>
                        <td><input type="text" name="ten_sp_kd_vi" id="ten_sp_kd_vi" class="tf" value="<?php echo $row_chitiet['ten_sp_kd_vi'] ?>"/></td>                        
                    </tr>
                    <tr>
                        <td>Tên SP EN</td>
                        <td><input type="text" name="ten_sp_en" id="ten_sp_en" class="tf" value="<?php echo $row_chitiet['ten_sp_en'] ?>" /></td>                        
                    </tr>
                    <tr>
                        <td>Tên SP KD EN</td>
                        <td><input type="text" name="ten_sp_kd_en" id="ten_sp_kd_en" class="tf" value="<?php echo $row_chitiet['ten_sp_kd_en'] ?>"/></td>                        
                    </tr>
                    <tr>
                    	<td>Khoảng giá</td>
                        <td colspan="3">
                            <select name="kg_id" id="kg_id" style="width: 200px">
                                <?php while($row_kg = mysql_fetch_assoc($kg)){ 
                                    if($row_kg['giatu']==0) $str = "Dưới ".number_format($row_kg['giaden']);
                                    elseif($row_kg['giaden']==50000000) $str = "Trên ".number_format($row_kg['giatu']);
                                    else $str= number_format($row_kg['giatu'])." đến ".number_format($row_kg['giaden']);
                                    
                                    ?>
                                <option <?php echo ($row_chitiet['kg_id']==$row_kg['kg_id'])? "selected=selected": ""; ?> value="<?php echo $row_kg['kg_id']; ?>"><?php echo $str; ?></option>
                                <?php } ?>
                            </select>                
                        </td>   
                    </tr> 
                </table>            
            </fieldset>
        </div>
        <div id="main_right" style="width:30%">
            <fieldset class="details_form">
                <legend>Tags VI</legend>
                <table>
                    <tr>                   
                        <td colspan="2"><textarea id="tags_vi" style="width:300px;height:100px" class="meta" name="tags_vi" rows="10" ><?php echo $strTagVi; ?></textarea></td>                        
                    </tr>                                        
                </table>
            </fieldset>
            <fieldset class="details_form">
                <legend>Tags EN</legend>
                <table>
                    <tr>                   
                        <td colspan="2"><textarea id="tags_en" style="width:300px;height:100px" class="meta" name="tags_en" rows="10" ><?php echo $strTagEn; ?></textarea></td>                        
                    </tr>                                        
                </table>
            </fieldset>
        </div>
        <div id="main_details" style="clear:both;padding-top: 20px">
            <fieldset class="details_form">
                <legend>Thông tin chi tiết</legend>
                <table border="1" cellspacing="0">
                    <tr>
                        <td width="15%">Giá</td>
                        <td><input type="text" name="gia" id="gia" class="tf" value="<?php echo $row_chitiet[gia] ?>" />
                            <span class="error"><?php echo $loi[gia]; ?></span>
                        </td>                        
                    </tr>  
                    <tr>
                        <td width="15%">Khuyến mãi</td>
                        <td>
                            <select name="khuyen_mai">
                                <option value="0">--Chọn--</option>
                                <?php for ($i = 5; $i < 100; $i = $i + 5) { ?>
                                    <option value="<?php echo $i; ?>"
                                    <?php if ($row_chitiet['khuyen_mai'] == $i) echo "selected"; ?>
                                            ><?php echo $i; ?></option>
                                        <?php } ?>
                            </select> %               
                        </td>                        
                    </tr>                                
                    <tr>
                        <td width="15%">Ngày hết hạn</td>
                        <td><input type="text" name="ngay" id="ngay" class="tf ngay" 
                                   value="<?php echo date('d-m-Y', $row_chitiet['ngay']); ?>" />
                            <span class="error"><?php echo $loi['ngay']; ?></span>
                        </td>                        
                    </tr>               
                    <tr>
                        <td>Upload hình - chữ nhật</td>
                        <td>
                            <input type="hidden" name="hinh_anh_cu" value="<?php echo $row_chitiet['hinh_anh']; ?>" />
                            <div style="width:100%;height:160px">
                                <?php
                                if (!empty($arrImg)) {
                                    foreach ($arrImg as $img) {
                                        ?>
                                        <div style="width:100px;float:left;height:140px;margin-right:10px;text-align:center">
                                            <img src="../<?php echo $img; ?>" width="65" height="83" /><br /><br />


                                            <input type='radio' name='hinh_dai_dien' value='../<?php echo $img; ?>' 
                                            <?php if ($row_chitiet['hinh_dai_dien'] == $img) echo "checked"; ?>

                                                   />
                                            <br /><br />
                                            <span class="xoa_img" style="cursor:pointer">Xóa hình</span>
                                        </div>
                                    <?php }
                                }
                                ?> 
                            </div>
                            <div style="clear:both"></div>
                            <div id="hinhanh">

                            </div>
                            <div style="clear:both"></div>
                            <input type="button" id="btnUpload" value="Thêm hình" />

                        </td>                        
                    </tr>  
                    <tr>
                        <td>Upload hình - vuông</td>
                        <td>
                            <input type="hidden" name="hinh_anh_cu_vuong" value="<?php echo $row_chitiet['hinh_anh_vuong']; ?>" />
                            <div style="width:100%;height:150px">
                                <?php
                                if (!empty($arrImg_vuong)) {
                                    foreach ($arrImg_vuong as $img) {
                                        ?>
                                        <div style="width:100px;float:left;height:120px;margin-right:10px;text-align:center">
                                            <img src="../<?php echo $img; ?>" width="70" height="70" /><br /><br />
                                            <input type='radio' name='hinh_dai_dien_vuong' value='../<?php echo $img; ?>' 
                                            <?php if ($row_chitiet['hinh_dai_dien_vuong'] == $img) echo "checked"; ?>

                                                   />
                                            <br /><br />
                                            <span class="xoa_img_2" style="cursor:pointer">Xóa hình</span>
                                        </div>
                                    <?php }
                                }
                                ?> 
                            </div>
                            <div style="clear:both"></div>
                            <div id="hinhanh_2">

                            </div>
                            <div style="clear:both"></div>
                            <input type="button" id="btnUpload_2" value="Thêm hình" />

                        </td>                        
                    </tr> 
                    <tr>
                        <td>Size</td>
                        <td>
                            <input type="checkbox" id="size_all" name="size_all" value="all" /> All size<br /><br />
                            <table style="width:500px" cellpadding="0" cellspacing="0">
                                <tr>
                                    <?php
                                    $list_size = $size->getListSize();
                                    while ($row_size = mysql_fetch_assoc($list_size)) {
                                        ?>
                                        <td style="width:10px;padding:0px">
                                            <input class="size" type="checkbox" name="size[]" value="<?php echo $row_size['size_id']; ?>" <?php if (in_array($row_size['size_id'], $arrSize) == true) echo "checked"; ?> /><br />
                                        <?php echo $row_size['size']; ?></td>
<?php } ?>
                                </tr>
                            </table>
                        </td>                        
                    </tr>      
                    <tr>
                        <td>Color</td>
                        <td>
                            <input type="checkbox" id="color_all" name="color_all" value="all" /> All color<br /><br />
                            <table style="width:800px" cellpadding="0" cellspacing="0">
                                <tr>
                                    <?php
                                    $list_mau = $mau->getListMau();
                                    while ($row_mau = mysql_fetch_assoc($list_mau)) {
                                        ?>
                                        <td style="width:20px;padding:0px;"><input class="mau" type="checkbox" name="mau[]" value="<?php echo $row_mau['mau_id']; ?>" <?php if (in_array($row_mau['mau_id'], $arrMau) == true) echo "checked"; ?> /><br />
                                            <img  src="<?php echo "../" . str_replace('mausac', 'mausac/small', $row_mau['hinh']); ?>" />

                                        </td>
<?php } ?>

                                </tr>
                            </table>
                        </td>                        
                    </tr>                    
                </table>
            </fieldset>

        </div>
        <div id="main_details" style="clear:both;padding-top: 20px">
            <fieldset class="details_form">
                <legend>++ Nội dung bài viết ++</legend>
                <table>                          
                    <tr>
                        <td>Mô tả VI</td>
                        <td>
                            <textarea style="height:100px;width:500px" class="meta" name="mo_ta_vi" id="mo_ta_vi" rows="10"><?php echo $row_chitiet['mo_ta_vi'] ?></textarea>                       
                        </td>                        
                    </tr> 
                    <tr>
                        <td>Mô tả EN</td>
                        <td>
                            <textarea style="height:100px;width:500px" class="meta" name="mo_ta_en" id="mo_ta_en" rows="10"><?php echo $row_chitiet['mo_ta_en'] ?></textarea>                       
                        </td>                        
                    </tr> 
                    <tr>
                        <td>Chi tiết VI</td>
                        <td>
                            <div style="width:800px;overflow: hidden">
                                <textarea class="meta" name="chi_tiet_vi" id="chi_tiet_vi"><?php echo $row_chitiet['chi_tiet_vi'] ?></textarea>
                                <script type="text/javascript">
    var editor = CKEDITOR.replace('chi_tiet_vi', {
        uiColor: '#9AB8F3',
        language: 'vi',
        skin: 'office2003',
        filebrowserImageBrowseUrl: 'ckfinder/ckfinder.html?Type=Images',
        filebrowserFlashBrowseUrl: 'ckfinder/ckfinder.html?Type=Flash',
        filebrowserImageUploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
        filebrowserFlashUploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
        toolbar: [
            ['Source', '-', 'Bold', 'Italic', 'Underline', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', 'Link', 'Unlink', 'Image', 'Styles', 'Format', 'TextColor', 'BGColor'],
        ]
    });
                                </script>
                            </div>          
                        </td>                        
                    </tr> 
                    <tr>
                        <td>Chi tiết EN</td>
                        <td>
                            <div style="width:800px;overflow: hidden">
                                <textarea class="meta" name="chi_tiet_en" id="chi_tiet_en"><?php echo $row_chitiet['chi_tiet_en'] ?></textarea>
                                <script type="text/javascript">
    var editor = CKEDITOR.replace('chi_tiet_en', {
        uiColor: '#9AB8F3',
        language: 'vi',
        skin: 'office2003',
        filebrowserImageBrowseUrl: 'ckfinder/ckfinder.html?Type=Images',
        filebrowserFlashBrowseUrl: 'ckfinder/ckfinder.html?Type=Flash',
        filebrowserImageUploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
        filebrowserFlashUploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
        toolbar: [
            ['Source', '-', 'Bold', 'Italic', 'Underline', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', 'Link', 'Unlink', 'Image', 'Styles', 'Format', 'TextColor', 'BGColor'],
        ]
    });
                                </script>
                            </div>          
                        </td>                        
                    </tr> 

                </table>
            </fieldset>

        </div>

        <div class="clr"></div>
    </div>
</form>
<div id="div_upload" style="display:none">
    <form id="upload_images" method="post" enctype="multipart/form-data" enctype="multipart/form-data" action="ajax.php">
        <div style="margin: auto;">       
            <img src="img/add.jpg" id="add_images" width="32" />
            <div id="wrapper_input_files">
                <input type="file" name="images[]" /><br />
                <input type="file" name="images[]" /><br />
                <input type="file" name="images[]" /><br />
            </div>            
            <div class="clear"></div>       
            <button style="margin-top: 10px;"class="button_colour" type="submit" id="btn_upload_images">                
                <a href="#">Upload</a>
            </button>        
        </div>

    </form>
</div>
<div id="div_upload_2" style="display:none">
    <form id="upload_images_2" method="post" enctype="multipart/form-data" enctype="multipart/form-data" action="ajax_2.php">
        <div style="margin: auto;">       
            <img src="img/add.jpg" id="add_images_2" width="32" />
            <div id="wrapper_input_files_2">
                <input type="file" name="images[]" /><br />
                <input type="file" name="images[]" /><br />
                <input type="file" name="images[]" /><br />
            </div>            
            <div class="clear"></div>       
            <button style="margin-top: 10px;"class="button_colour" type="submit" id="btn_upload_images_2">                
                <a href="#">Upload</a>
            </button>        
        </div>

    </form>
</div>
<script type="text/javascript" src="js/form.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="js/jquery-ui.custom.js"></script>
<script type="text/javascript" src="js/jquery.ui.dialog.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery.ui.base.css"/>
<link rel="stylesheet" type="text/css" href="css/jquery.ui.theme.css"/>
<script type="text/javascript">
    $(function(){ 
        $('#loai_id').change(function(){
            var loai_id = $(this).val();
            $.post("ajax/getkieu.php", {loai_id: loai_id}, function(data) {
                $("tr#row_kieu").html(data);
            });
        });
        $(".ngay").datepicker({
            numberOfMonths: 1,  dateFormat: 'dd-mm-yy',
            monthNames: ['Một','Hai','Ba','Tư','Năm','Sáu','Bảy','Tám','Chín', 
            'Mười','Mười một','Mười hai'] ,
            monthNamesShort: ['Tháng1','Tháng2','Tháng3','Tháng4','Tháng5',
            'Tháng6','Tháng7','Tháng8','Tháng9','Tháng10','Tháng11','Tháng12'] ,
            dayNames: ['Chủ nhật', 'Thứ hai', 'Thứ ba', 'Thứ tư', 'Thứ năm',
            'Thứ sáu', 'Thứ bảy'],
            dayNamesMin: ['CN', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7'] ,
            showWeek: true , showOn: 'both',
            changeMonth: true , changeYear: true,
            currentText: 'Hôm nay', weekHeader: 'Tuần',
			minDate: 1,
            
        });        
    });
    
</script>